import 'dart:async';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;

import 'water_fallflow_route.dart';

class TVPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    return TVPageState();
  }
}

class TVPageState extends State<TVPage> {
  final SwiperController _controller = new SwiperController();
  late Timer timer;
  bool init = false;
  String timeString = '';
  final List<String> tabs = ["重磅热播", "推荐", "电影", "电视剧", "综艺", "动漫", "短剧"];
  final List<int> types = [-1, 1, 2, 3, 4, 26];
  int selectedIndex = 0;


  @override
  void initState() {
    tz.initializeTimeZones();
    tz.Location location = tz.getLocation('Asia/Shanghai');
    startTimeout(location);
    timeString = TimeUtils.getTime(location);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black87,
      padding: EdgeInsets.all(30),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black87,
          leading: Icon(
            Icons.live_tv,
            color: Colors.deepOrange,
            size: 50,
          ),
          title: Text(
            '芒果TV',
            style: TextStyle(
                fontSize: 30, color: Colors.white, fontStyle: FontStyle.italic),
          ),
          primary: true,
          actions: <Widget>[
            Padding(
              padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
              child: Text(
                '$timeString',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ],
        ),
        body: Column(
          children: [
            Expanded(
              flex: 1,
              child: Container(
                color: Colors.black87,
                child: Row(
                  children: List.generate(
                    tabs.length,
                    (i) => Expanded(child: focusCard(i)),
                  ),
                ),
              ),
            ),
            Expanded(
              flex: 9,
              child: Container(
                decoration: BoxDecoration(color: Colors.white),
                child: Swiper(
                  itemCount: types.length,
                  itemBuilder: (BuildContext context, int i) =>
                      WaterfallFlowRoute(tid: types.elementAt(i)),
                  loop: false,
                  controller: _controller,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget focusCard(index) {
    return Focus(
      onFocusChange: (focused) {
        setState(() {
          selectedIndex = focused ? index : selectedIndex;
          _controller.move(selectedIndex);
        });
      },
      child: Container(
        padding: EdgeInsets.only(left: 20, right: 20, bottom: 10),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: selectedIndex == index
                  ? Colors.deepOrange
                  : Colors.transparent,
              width: 3,
              style: BorderStyle.solid,
            ),
          ),
        ),
        margin: EdgeInsets.all(2),
        child: Align(
          child: Container(
            child: Text(
              tabs[index],
              style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }



  void startTimeout(tz.Location location) {
    timer = Timer(
      Duration(minutes: 1),
          () => setState(() {
        timeString = TimeUtils.getTime(location);
      }),
    );
  }

}

class TimeUtils {
  static String getTime(tz.Location location) {
    DateTime dateTime = DateTime.now();
    tz.TZDateTime beijingTime = tz.TZDateTime.from(dateTime, location);
    String timeString =
        '${beijingTime.year}年${beijingTime.month}月${beijingTime.day}日 ${beijingTime.hour}:${beijingTime.minute}';
    return timeString;
  }
}
